package com.zone.domain.strategy.service.rule.tree.impl;

import com.zone.domain.strategy.model.valobj.RuleActionEnum;
import com.zone.domain.strategy.service.rule.tree.ILogicTreeNode;
import com.zone.domain.strategy.service.rule.tree.factory.DefaultTreeNodeFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Component;

/**
 * @author: zongzi
 * @description: 兜底，幸运奖
 * @date: 2024/6/11
 */
@Slf4j
@Component("rule_luck_award")
public class RuleLuckAwardTreeNode implements ILogicTreeNode {

    @Override
    public DefaultTreeNodeFactory.TreeNodeActionEntity logic(Long strategyId, String userId, Integer awardId, String ruleValue) {
        // 101:10,60
        String luckAwardIdStr = StringUtils.substringBefore(ruleValue, ":");
        Integer luckAwardId = Strings.isNotEmpty(luckAwardIdStr) ? Integer.valueOf(luckAwardIdStr) : null;

        String awardRuleValue = StringUtils.substringAfter(ruleValue, ":");
        return DefaultTreeNodeFactory.TreeNodeActionEntity.builder()
                .ruleAction(RuleActionEnum.INTERCEPT)
                .strategyAward(DefaultTreeNodeFactory.StrategyAwardVO.builder()
                        .awardId(luckAwardId)
                        .awardRuleValue(awardRuleValue)
                        .build())
                .build();
    }
}
